<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" @submit.native.prevent>
      <el-form-item label="关键字" prop="search" label-width="68px">
        <el-input v-model="queryParams.search" placeholder="UID、昵称、电话、编号,名称" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="转售状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="转售状态" clearable style="width: 240px">
          <el-option v-for="dict in resellStateList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="场馆" prop="venueId" label-width="68px">
        <el-select v-model="queryParams.venueId" placeholder="场馆选择" clearable filterable style="width: 240px">
          <el-option v-for="dict in venueList" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 340px" type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-download" size="mini" @click="exportExcel" v-hasPermi="['order:resale:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" @sort-change="changeTableSort" :data="preList" @row-dblclick="handleUpdate" stripe border>
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="购买时间" prop="buyDate" align="center" width="160" :sortable="'custom'" />
      <el-table-column label="转售时间" prop="createTime" align="center" width="160" :sortable="'custom'" />
      <el-table-column label="场馆" prop="newVenueName" align="center" width="120" />
      <el-table-column label="商品编号" prop="goodsNo" align="center" width="120" :sortable="'custom'" />
      <el-table-column label="商品名称" prop="goodsName" align="center" width="180" :sortable="'custom'" />
      <el-table-column label="卖方UID" prop="resellUserId" align="center" width="100" :sortable="'custom'" />
      <el-table-column label="转售价格" prop="resellPrice" align="center" width="120" :sortable="'custom'" />
      <el-table-column label="手续费" prop="resellCharge" align="center" width="120" :sortable="'custom'" />
      <el-table-column label="手续费类型" prop="resellChargeType" align="center" width="120" :sortable="'custom'">
        <template slot-scope="scope">
          {{scope.row.resellChargeType==1?'现金':'金积分'}}
        </template>
      </el-table-column>
      <el-table-column label="成本价(元)" prop="costPrice" width="120" align="center" :sortable="'custom'" v-if="checkPermi(['order:resale:lookprice'])" />
      <el-table-column label="转售状态" prop="resellState" align="center" width="120" :sortable="'custom'">
        <template slot-scope="scope">
          {{getLabel(resellState,scope.row.resellState)}}
        </template>
      </el-table-column>
      <el-table-column label="买方UID" prop="buyUserId" align="center" width="100" :sortable="'custom'" />
      <el-table-column label="买入价格" prop="buyPrice" align="center" width="120" :sortable="'custom'" />
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="" @click="handleUpdate(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <Z-Dialog :title="title" :visible.sync="open" @confirm="submitForm">
      <form-view :form="form" ref="form"></form-view>
    </Z-Dialog>

  </div>
</template>

<script>
import { listOrderResell } from "@/api/order/order.js";
import { resellState } from '@/common/enums'
import formView from "./formView.vue";
import { getLabelByValue } from "@/common/common.js";
import { excelExportApi } from '@/api/filer';
import { mapState } from 'vuex'
import { checkPermi } from "@/utils/permission"; // 权限判断函数

export default {
  name: "Resale", //订单转售
  components: { formView },

  dicts: ["order_resale_status"],
  data() {
    return {
      resellState,
      // 列表数据
      preList: [],
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
        preId: undefined,
      },
      pageNum: 1,
      // 表单参数
      form: {},

      // 日期范围
      dateRange: [],

    };
  },

  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {

        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
    dateRange(newV, oldV) {
      if (newV) {
        if (newV.length >= 2) {
          this.queryParams.createStartTime = String(newV[0]);
          this.queryParams.createEndTime = String(newV[1]);
        }
      } else {
        this.queryParams.createStartTime = undefined;
        this.queryParams.createEndTime = undefined;
      }
      this.getList();
    },
  },

  computed: {
    ...mapState({
      venueList: state => state.venue.list
    }),
    tableHeight() {
      return 'calc(100vh - 280px)'
    },
    //转售状态
    resellStateList() {
      let typeDictArray = JSON.parse(JSON.stringify(resellState));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },

  },
  created() {
    this.getList();
    this.$store.dispatch('GetVenueList')
  },
  methods: {
    checkPermi,
    getLabel(arr, value) {
      return getLabelByValue(arr, value);
    },

    getList() {
      this.loading = true;
      listOrderResell(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.preList = response.data;
          this.total = response.total;
        }
        this.loading = false;
      });
    },

    changeTableSort(column) {
      this.queryParams.orderBy = column.prop == 'venueId' ? 'venueName' : column.prop;
      this.queryParams.isAsc = (column.order == 'descending' ? 0 : 1); //ascending == 升序 descending == 降序  0 倒,1正
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.createTimeStart = undefined;
      this.queryParams.createTimeEnd = undefined;
      this.dateRange = undefined;
      this.handleQuery();
    },

    /** 查看按钮操作 */
    handleUpdate(row) {
      //   this.reset();
      this.open = true;
      this.title = '转售订单';
      this.form = row;
    },
    submitForm() {
      this.open = false;
    },

    /**导出 */
    exportExcel() {
      this.queryParams.beanName = 'OrderResellExport',
        this.queryParams.fileName = '转售订单',
        this.$alert('需要导出 "' + this.total + '" 条数据,是否确认导出', '提示', {
          confirmButtonText: '确定',

        }).then(() => {
          excelExportApi(this.queryParams, '/analy');
        });
    },
  },
};
</script>
<style scoped>
</style>
